// TCP 文件传输
const net = require('net')
const fs = require('fs')

const netServer = net.createServer()

let clientList = []

netServer.on('connection', (client) => {
    console.log('发现新连接')

    // 文件传输2.0
    let ws = fs.createWriteStream(`./img-${Math.round(Math.random() * 1000)}.png`)
    client.pipe(ws)

    // 文件传输1.0
    // let chunkList = []
    // client.on('data', (data) => {
    //     chunkList.push(data)
    // })
    // client.on('end', () => {
    //     let data = Buffer.concat(chunkList)
    //     // console.log('服务端接收到: ', data)
    //     fs.writeFile(`./img-${Math.round(Math.random() * 1000)}.png`, data, (err) => {
    //         if (!err) {
    //             console.log('写入成功')
    //         }
    //     })
    // })
})

netServer.listen(8000, () => {
    console.log('server is runing')
})
